<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="SlopeRiskEvaluationSettingForm">
          <a-row>
						<a-col :span="24">
							<a-form-item label="切坡墙距权重" v-bind="validateInfos.slopeWallDistanceWeight" id="SlopeRiskEvaluationSettingForm-slopeWallDistanceWeight" name="slopeWallDistanceWeight">
								<a-input-number v-model:value="formData.slopeWallDistanceWeight" placeholder="请输入切坡墙距权重" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡墙距 A＜1" v-bind="validateInfos.slopeWallDistance1" id="SlopeRiskEvaluationSettingForm-slopeWallDistance1" name="slopeWallDistance1">
								<a-input-number v-model:value="formData.slopeWallDistance1" placeholder="请输入切坡墙距 A＜1" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡墙距 1≤A＜2" v-bind="validateInfos.slopeWallDistance2" id="SlopeRiskEvaluationSettingForm-slopeWallDistance2" name="slopeWallDistance2">
								<a-input-number v-model:value="formData.slopeWallDistance2" placeholder="请输入切坡墙距 1≤A＜2" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡墙距 2≤A＜4" v-bind="validateInfos.slopeWallDistance3" id="SlopeRiskEvaluationSettingForm-slopeWallDistance3" name="slopeWallDistance3">
								<a-input-number v-model:value="formData.slopeWallDistance3" placeholder="请输入切坡墙距 2≤A＜4" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡墙距 4≤A" v-bind="validateInfos.slopeWallDistance4" id="SlopeRiskEvaluationSettingForm-slopeWallDistance4" name="slopeWallDistance4">
								<a-input-number v-model:value="formData.slopeWallDistance4" placeholder="请输入切坡墙距 4≤A" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡时间权重" v-bind="validateInfos.cutSlopeTimeWeight" id="SlopeRiskEvaluationSettingForm-cutSlopeTimeWeight" name="cutSlopeTimeWeight">
								<a-input-number v-model:value="formData.cutSlopeTimeWeight" placeholder="请输入切坡时间权重" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡时间 2013年以前" v-bind="validateInfos.cutSlopeTime1" id="SlopeRiskEvaluationSettingForm-cutSlopeTime1" name="cutSlopeTime1">
								<a-input-number v-model:value="formData.cutSlopeTime1" placeholder="请输入切坡时间 2013年以前" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡时间 2013年以后" v-bind="validateInfos.cutSlopeTime2" id="SlopeRiskEvaluationSettingForm-cutSlopeTime2" name="cutSlopeTime2">
								<a-input-number v-model:value="formData.cutSlopeTime2" placeholder="请输入切坡时间 2013年以后" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡宽度权重" v-bind="validateInfos.cutSlopeWidthWeight" id="SlopeRiskEvaluationSettingForm-cutSlopeWidthWeight" name="cutSlopeWidthWeight">
								<a-input-number v-model:value="formData.cutSlopeWidthWeight" placeholder="请输入切坡宽度权重" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡宽度 B≤25m" v-bind="validateInfos.cutSlopeWidth1" id="SlopeRiskEvaluationSettingForm-cutSlopeWidth1" name="cutSlopeWidth1">
								<a-input-number v-model:value="formData.cutSlopeWidth1" placeholder="请输入切坡宽度 B≤25m" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡宽度 25m＜B≤50m" v-bind="validateInfos.cutSlopeWidth2" id="SlopeRiskEvaluationSettingForm-cutSlopeWidth2" name="cutSlopeWidth2">
								<a-input-number v-model:value="formData.cutSlopeWidth2" placeholder="请输入切坡宽度 25m＜B≤50m" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡宽度 50m＜B≤100m" v-bind="validateInfos.cutSlopeWidth3" id="SlopeRiskEvaluationSettingForm-cutSlopeWidth3" name="cutSlopeWidth3">
								<a-input-number v-model:value="formData.cutSlopeWidth3" placeholder="请输入切坡宽度 50m＜B≤100m" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="切坡宽度 B＞100m" v-bind="validateInfos.cutSlopeWidth4" id="SlopeRiskEvaluationSettingForm-cutSlopeWidth4" name="cutSlopeWidth4">
								<a-input-number v-model:value="formData.cutSlopeWidth4" placeholder="请输入切坡宽度 B＞100m" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁人口权重" v-bind="validateInfos.threatPopulationWeight" id="SlopeRiskEvaluationSettingForm-threatPopulationWeight" name="threatPopulationWeight">
								<a-input-number v-model:value="formData.threatPopulationWeight" placeholder="请输入威胁人口权重" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁人口 C＜3" v-bind="validateInfos.threatPopulation1" id="SlopeRiskEvaluationSettingForm-threatPopulation1" name="threatPopulation1">
								<a-input-number v-model:value="formData.threatPopulation1" placeholder="请输入威胁人口 C＜3" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁人口 3≤C＜10" v-bind="validateInfos.threatPopulation2" id="SlopeRiskEvaluationSettingForm-threatPopulation2" name="threatPopulation2">
								<a-input-number v-model:value="formData.threatPopulation2" placeholder="请输入威胁人口 3≤C＜10" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁人口 10≤C＜30" v-bind="validateInfos.threatPopulation3" id="SlopeRiskEvaluationSettingForm-threatPopulation3" name="threatPopulation3">
								<a-input-number v-model:value="formData.threatPopulation3" placeholder="请输入威胁人口 10≤C＜30" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁人口 30≤C" v-bind="validateInfos.threatPopulation4" id="SlopeRiskEvaluationSettingForm-threatPopulation4" name="threatPopulation4">
								<a-input-number v-model:value="formData.threatPopulation4" placeholder="请输入威胁人口 30≤C" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁房屋结构权重" v-bind="validateInfos.threatHouseWeight" id="SlopeRiskEvaluationSettingForm-threatHouseWeight" name="threatHouseWeight">
								<a-input-number v-model:value="formData.threatHouseWeight" placeholder="请输入威胁房屋结构权重" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁房屋结构 钢混及钢结构" v-bind="validateInfos.threatHouse1" id="SlopeRiskEvaluationSettingForm-threatHouse1" name="threatHouse1">
								<a-input-number v-model:value="formData.threatHouse1" placeholder="请输入威胁房屋结构 钢混及钢结构" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁房屋结构 砖混" v-bind="validateInfos.threatHouse2" id="SlopeRiskEvaluationSettingForm-threatHouse2" name="threatHouse2">
								<a-input-number v-model:value="formData.threatHouse2" placeholder="请输入威胁房屋结构 砖混" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁房屋结构 砖木" v-bind="validateInfos.threatHouse3" id="SlopeRiskEvaluationSettingForm-threatHouse3" name="threatHouse3">
								<a-input-number v-model:value="formData.threatHouse3" placeholder="请输入威胁房屋结构 砖木" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="威胁房屋结构 土木及土坯" v-bind="validateInfos.threatHouse4" id="SlopeRiskEvaluationSettingForm-threatHouse4" name="threatHouse4">
								<a-input-number v-model:value="formData.threatHouse4" placeholder="请输入威胁房屋结构 土木及土坯" style="width: 100%" />
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../SlopeRiskEvaluationSetting.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    slopeWallDistanceWeight: undefined,
    slopeWallDistance1: undefined,
    slopeWallDistance2: undefined,
    slopeWallDistance3: undefined,
    slopeWallDistance4: undefined,
    cutSlopeTimeWeight: undefined,
    cutSlopeTime1: undefined,
    cutSlopeTime2: undefined,
    cutSlopeWidthWeight: undefined,
    cutSlopeWidth1: undefined,
    cutSlopeWidth2: undefined,
    cutSlopeWidth3: undefined,
    cutSlopeWidth4: undefined,
    threatPopulationWeight: undefined,
    threatPopulation1: undefined,
    threatPopulation2: undefined,
    threatPopulation3: undefined,
    threatPopulation4: undefined,
    threatHouseWeight: undefined,
    threatHouse1: undefined,
    threatHouse2: undefined,
    threatHouse3: undefined,
    threatHouse4: undefined,
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
